package fdoj.eventInvitations.entity;

import static javax.persistence.CascadeType.ALL;
import static javax.persistence.FetchType.EAGER;
import static javax.persistence.GenerationType.IDENTITY;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
@Entity
@Table(name="events")
public class Event implements Serializable{
	/**
	 *
	 */
	private static final long serialVersionUID = 6738980922071809209L;

	@Id
	@GeneratedValue(strategy=IDENTITY)
	@Column(name="event_id", unique=true, nullable=false)
	private Integer id;

	@Column(name="title", unique=false, nullable=true)
	private String title;

	@Column(name="description", unique=false, nullable=true)
	private String description;

	@Column(name="startTime", unique=false, nullable=true)
	private Date startTime;//date je util

	@Column(name="endTime", unique=false, nullable=true)
	private Date endTime;//date je util, opciono

	@Column(name="checkTime", unique=false, nullable=true)
	private Date checkTime;//date je util, opciono

	@Column(name="place", unique=false, nullable=true)
	private String place;
	
	@Column(name="canceld", unique=false, nullable=true)
	private Boolean canceled;


	@Column(name="image", unique=false, nullable=true)
	private String image;

	@OneToMany(cascade={ALL}, fetch=EAGER, mappedBy="comment")
	private Set<Comment> comments = new HashSet<Comment>();

	@OneToMany(cascade={ALL}, fetch=EAGER, mappedBy="event")
	private Set<Invantation> invitations = new HashSet<Invantation>();

	@ManyToOne
	@JoinColumn(name="owner_id", referencedColumnName="person_id", nullable=false)
	private Person owner;
	
	@Transient
	private List<Contact> attending = new ArrayList<Contact>();
	
	@Transient
	private List<Contact> maybeAttending = new ArrayList<Contact>();
	
	@Transient
	private List<Contact> notAttending = new ArrayList<Contact>();
	
	@Transient
	private List<Contact> unanswered= new ArrayList<Contact>();
	
	public void addComment(Comment comment) {
	    if (comment.getComment() != null)
	    	comment.getComment().getComments().remove(comment);
	    comment.setComment(this);
	    getComments().add(comment);
	  }
	public void addInvitation(Invantation invitation) {
	    if (invitation.getEvent() != null)
	    	invitation.getEvent().getInvitations().remove(invitation);
	    invitation.setEvent(this);
	    getInvitations().add(invitation);
	  }
	
	public void removeInvitation(Invantation invitation){
		invitation.setEvent(null);
		invitations.remove(invitation);
	}
	
	public void removeComment(Comment comment){
		comment.setComment(null);
		comments.remove(comment);
	}

	public Event(){

	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public Date getCheckTime() {
		return checkTime;
	}

	public void setCheckTime(Date checkTime) {
		this.checkTime = checkTime;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public String getImage() {
		return image;
	}

	public void setImage(String image) {
		this.image = image;
	}

	public Set<Comment> getComments() {
		return comments;
	}

	public void setComments(Set<Comment> comments) {
		this.comments = comments;
	}

	public Person getOwner() {
		return owner;
	}

	public void setOwner(Person owner) {
		this.owner = owner;
	}
	public Set<Invantation> getInvitations() {
		return invitations;
	}
	public void setInvitations(Set<Invantation> invitation) {
		this.invitations = invitation;
	}
	public List<Comment> getSortedComments(){
		List<Comment> l = new ArrayList<Comment>(comments);
		dateTopDown(l, true);
		return l;
	}
	private void dateTopDown(List<Comment> list, boolean topDown){
		Comment temp;
		for(int i=0; i< list.size(); i++){
			for(int j=i+1; j< list.size(); j++){
				if(topDown){// ako se ide od najmladjeg
					if(list.get(i).getDate().after(list.get(j).getDate())){
						temp = list.get(j);
						list.set(j, list.get(i)) ;
						list.set(i, temp);
					}
				}else{
					if(list.get(i).getDate().before(list.get(j).getDate())){
						temp = list.get(j);
						list.set(j, list.get(i)) ;
						list.set(i, temp);
					}
				}
			}
		}
	}
	public List<Contact> getAttending() {
		return attending;
	}
	public void setAttending(List<Contact> attending) {
		this.attending = attending;
	}
	public List<Contact> getMaybeAttending() {
		return maybeAttending;
	}
	public void setMaybeAttending(List<Contact> maybeAttending) {
		this.maybeAttending = maybeAttending;
	}
	public List<Contact> getNotAttending() {
		return notAttending;
	}
	public void setNotAttending(List<Contact> notAttending) {
		this.notAttending = notAttending;
	}
	public void setUnanswered(List<Contact> unanswered) {
		this.unanswered = unanswered;
	}
	public List<Contact> getUnanswered() {
		return unanswered;
	}
	public Boolean getCanceled() {
		return canceled;
	}
	public void setCanceled(Boolean canceled) {
		this.canceled = canceled;
	}
	

}
